package com.roper.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.roper.yygh.cmn.client.DictFeignClient;
import com.roper.yygh.hosp.repository.HospReopsitory;
import com.roper.yygh.hosp.service.HospitalService;
import com.roper.yygh.model.hosp.Hospital;
import com.roper.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 医院接口
 * Date:      2021/7/10 21:08
 * author:    LPH
 * version:   V1.0
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospReopsitory hospReopsitory;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramsMap) {
        // 先讲map转换为对象
        String mapString = JSONObject.toJSONString(paramsMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        // 判读数据库中是否已经存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospReopsitory.getHospitalByHoscode(hoscode);

        // 存在，更新
        if(hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospReopsitory.save(hospital);
        }else {
            // 不存在，插入
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospReopsitory.insert(hospital);
        }

    }

    // 根据医院编号进行查询医院信息
    @Override
    public Hospital getByHoscode(String hosCode) {
        return hospReopsitory.getHospitalByHoscode(hosCode);
    }

    // 条件查询带分页查询医院列表
    @Override
    public Page<Hospital> selectHospPage(int page, int limit, HospitalQueryVo hospitalQueryVo) {
        // 创建Pageable对象
        PageRequest pageRequest = PageRequest.of(page - 1, limit);
        // 创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        // hospitalQueryVo 进行转换
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        // 创建Example对象
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> pages = hospReopsitory.findAll(example, pageRequest);

        // 使用java8的流方式写 获取hospital的list集合，遍历进行医院等级的封装
        pages.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });

        return pages;
    }

    @Override
    public Hospital showHosp(String id) {
        return hospReopsitory.getHospitalById(id);
    }

    @Override
    public void updateHospStatus(String id, Integer status) {
        Hospital hospital = hospReopsitory.getHospitalById(id);
        hospital.setStatus(status);
        hospReopsitory.save(hospital);
    }

    // 去cmn Model中远程调用    将医院列表中的code转换为字符串
    private Hospital setHospitalHosType(Hospital hospital) {
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        Map<String, Object> map = new HashMap<>();

        // 查询省市 市区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        map.put("hostypeString", hostypeString);
        map.put("fulAddress", provinceString + cityString + districtString);
        hospital.setParam(map);
        return hospital;
    }
}
